﻿using System;
using System.Web.UI;
using BLL;
using Model;

public partial class ProductAirtightness_AirtightnessAdd : System.Web.UI.Page
{
    private Airtightness a;
    private ProductType_Airtightness pa;
    private ProductType_Flow pf;

    public ProductAirtightness_AirtightnessAdd()
    {
        a = new Airtightness();
        pa = new ProductType_Airtightness();
        pf = new ProductType_Flow();
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
        string pid = this.TextBox2.Text.Trim();
        ProductType_AirtightnessInfo pai = pa.ProductType_Airtightness_GetbyPIDCHNO(pid,1);
        if (string.IsNullOrEmpty(pai.ProductTypeID))
        {
            string str = "此产品还没有相应通道号的规格值，请联系相关人员！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            this.Label1.Text = "";
            return;
        }
        this.Label1.Text = pai.SpecValue.ToString();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(this.Label1.Text.Trim()))
        {
            string str = "请先扫描产品类型S代码，调出相应的规格值";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            return;
        }
        string sn = this.TextBox1.Text.Trim();
        string pid = this.TextBox2.Text.Trim();
        if (!pf.ProductType_Flow_HasValidSevenStep(sn, pid, "AirtightnessStep"))
        {
            string str = "不能进行气密测试，请反查此腔体的七大管控工序的数据！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            return;
        }
        try
        {
            int testvalue = int.Parse(this.TextBox4.Text.Trim());
            AirtightnessInfo ai = new AirtightnessInfo(sn, DateTime.Now, pid, User.Identity.Name, IPGet.getIp(), testvalue, false);
            a.Insert(ai);            
            FeedBack(sn);
            this.TextBox1.Text = "";
            this.TextBox4.Text = "";
            this.TextBox1.Focus();
        }
        catch (FormatException)
        {
            string str = "测试值格式输入不对，请检查！" ;
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            return;
        }
        catch
        {
            string str = "录入失败，请联系相关人员！" ;
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            return;
        }
    }

    protected void FeedBack(string sn)
    {
        AirtightnessInfo ai1 = a.LoadEntity(sn);
        string str;
        if (ai1.IsPass)
        {
            str = "合格";
        }
        else
        {
            str = "测试值大于规格值，测试结果：不合格";
        }
        ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        string sn = this.TextBox5.Text.Trim();
        this.GridView1.DataSource = a.Airtightness_SelectbySN(sn);
        this.GridView1.DataBind();
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            if (bool.Parse(GridView1.DataKeys[i].Values[0].ToString()))
            {
                this.GridView1.Rows[i].Cells[5].Text = "Pass";
            }
            else 
            {
                this.GridView1.Rows[i].Cells[5].Text = "Fail";
                this.GridView1.Rows[i].Cells[5].ForeColor = System.Drawing.Color.Red;
            }
        }
    }
}
